<?php

$conn = mysql_connect('studentdb.gl.umbc.edu', 'chrimo1', 'chrimo1');
if (!$conn) {
   die('Could not connect');
}

$db_selected = mysql_select_db('chrimo1', $conn);
if (!$db_selected) {
   die ('Can\'t use chrimo1');
}

echo 'Adding to Cart...';

$id = $_GET['id'];

$sql = "SELECT * FROM gameInventory WHERE idNum=".$id;


$result = mysql_query($sql, $conn);



if(mysql_num_rows($result) == 0){
	echo 'Game &nbsp Not &nbsp Found.';
	die();
}

$r = mysql_fetch_array($result);

$cost = $r['price'];
$id = $r['idNum'];
$quantity = $r['quantity'];
echo "<br>Quantity:".$quantity;

if($quantity >= 1){
	$sql1 = "INSERT INTO shoppingCart (  custIdNum ,  orderNum ,  gameId ,  quantity,  cost ) VALUES ('".$_COOKIE['user']."',  '1',  '".$id."',  '1',  '".$cost."')";
	if($insert = mysql_query($sql1, $conn)){
		$quantity = $quantity-1;
		echo "<br>Quantity:".$quantity;
		$sql2 = "UPDATE  gameInventory SET  quantity=".$quantity." WHERE idNum=".$id;
		if( $update = mysql_query($sql2, $conn)){
			echo 'Success';
		}else{
			echo "<br>Failure updating inventory";
		}
	}
	else{
		echo "<br>Failure Adding to cart";
	}
	

}else{
	echo "<br>Sold Out.";
}

echo '<br><a href="cart.php">Continue to Cart</a>';



?>